

package com.maoshi.shop.distribution.platform.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.maoshi.shop.common.util.PageParam;
import com.maoshi.shop.distribution.comment.dto.DistributionAuditingDto;
import com.maoshi.shop.distribution.comment.model.DistributionAuditing;
import com.maoshi.shop.distribution.comment.model.DistributionUser;
import com.maoshi.shop.distribution.comment.param.RangeTimeParam;
import com.maoshi.shop.distribution.comment.service.DistributionAuditingService;
import com.maoshi.shop.distribution.comment.service.DistributionUserService;
import com.maoshi.shop.security.platform.util.SecurityUtils;
import lombok.AllArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Date;

/**
 * @author lgh on 2019/04/01.
 */
@RestController
@RequestMapping("/distribution/distributionAuditing")
@AllArgsConstructor
public class DistributionAuditingController {

    private final DistributionAuditingService distributionAuditingService;

    private final DistributionUserService distributionUserService;


    /**
     * 分页获取
     */
    @GetMapping("/page")
    @PreAuthorize("@pms.hasPermission('distribution:distributionAuditing:page')")
    public ResponseEntity<IPage<DistributionAuditingDto>> page(DistributionAuditing distributionAuditing, RangeTimeParam rangeTimeParam,
                                                               String mobile, Integer startExpenseNumber, Integer endExpenseNumber,
                                                               Double startPayAmount, Double endPayAmount, PageParam<DistributionAuditing> page) {
        IPage<DistributionAuditingDto> list = distributionAuditingService.distributionAuditingPage(page, distributionAuditing, rangeTimeParam, startExpenseNumber, endExpenseNumber, startPayAmount, endPayAmount, mobile);
        return ResponseEntity.ok(list);
    }

    /**
     * 获取信息
     */
    @GetMapping("/info/{id}")
    @PreAuthorize("@pms.hasPermission('distribution:distributionAuditing:info')")
    public ResponseEntity<DistributionAuditing> info(@PathVariable("id") Long id) {
        DistributionAuditing distributionAuditing = distributionAuditingService.getById(id);
        return ResponseEntity.ok(distributionAuditing);
    }

    /**
     * 审核
     */
    @PutMapping
    @PreAuthorize("@pms.hasPermission('distribution:distributionAuditing:update')")
    public ResponseEntity<Void> update(@RequestBody @Valid DistributionAuditing distributionAuditing) {
        distributionAuditing.setUpdateTime(new Date());
        distributionAuditing.setModifier(SecurityUtils.getSysUser().getUserId());
        distributionAuditingService.examine(distributionAuditing);
        DistributionUser distributionUser = distributionUserService.getById(distributionAuditing.getDistributionUserId());
        distributionUserService.removeCacheByUserIdAndShopId(distributionUser.getUserId(), distributionAuditing.getShopId());
        distributionUserService.removeCacheByCardNo(distributionUser.getCardNo());
        return ResponseEntity.ok().build();
    }

//	/**
//	 * 保存
//	 */
//	@PostMapping
//	@PreAuthorize("@pms.hasPermission('distribution:distributionAuditing:save')")
//	public ResponseEntity<Void> save(@RequestBody @Valid DistributionAuditing distributionAuditing){
//		distributionAuditingService.save(distributionAuditing);
//		return ResponseEntity.ok().build();
//	}

//	/**
//	 * 删除
//	 */
//	@DeleteMapping("/{id}")
//	@PreAuthorize("@pms.hasPermission('distribution:distributionAuditing:delete')")
//	public ResponseEntity<Void> delete(@PathVariable Long id){
//		distributionAuditingService.removeById(id);
//		return ResponseEntity.ok().build();
//	}
}
